package com.jx.cashier.card.impl;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;

import com.jx.cashier.card.CardCallback;
import com.jx.cashier.card.PayDeviceType;
import com.jx.cashier.card.ReaderCard;
import com.sunmi.pay.usdk.aidl.AidlConstants;
import com.sunmi.pay.usdk.aidl.bean.CardInfo;
import com.sunmi.pay.usdk.aidl.bean.TransData;
import com.sunmi.pay.usdk.aidl.readcard.ReadCardCallback;
import com.sunmi.pay.usdk.aidl.readcard.ReadCardOpt;
import com.sunmi.pay.usdk.kernel.SunmiPayKernel;


public class SunmiPayReaderCardImpl implements ReaderCard {

    private SunmiPayKernel mSunmiPayKernel;
    private ReadCardOpt mReadCardOpt;

    private CardCallback callback;
    private int timeout = 15;
    private int cardType = AidlConstants.CardType.MIFARE.getValue() + AidlConstants.CardType.IC.getValue();

    @Override
    public PayDeviceType getPayDeviceType() {
        return PayDeviceType.SUN_MI_PAY;
    }

    @Override
    public boolean init(Activity activity) {
        mSunmiPayKernel = SunmiPayKernel.getInstance();
        mSunmiPayKernel.connectPayService(mConnCallback, activity.getApplicationContext());
        return true;
    }

    @Override
    public void setTimeOut(int timeout) {
        this.timeout = timeout;
    }

    @Override
    public void startRead() {
        try {
            TransData transData = new TransData();
            transData.amount = "1";
            transData.transType = "00";
            mReadCardOpt.initTransData(transData);
            mReadCardOpt.checkCard(cardType, readCardCallback, timeout);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stopRead() {
        try {
            mReadCardOpt.cancelCheckCard();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        //nothing to do
    }

    @Override
    public void onPause() {

    }

    @Override
    public void setReadCallback(CardCallback callback) {
        this.callback = callback;
    }

    @Override
    public void resolveIntent(Intent intent) {
        //nothing to do
    }

    /**
     * 连接状态回调
     */
    private SunmiPayKernel.ConnectCallback mConnCallback = new SunmiPayKernel.ConnectCallback() {
        @Override
        public void onServiceConnected() {
            Log.e("wl", "连接成功");
            try {
                mReadCardOpt = mSunmiPayKernel.mReadCardOpt;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected() {
            Log.e("wl", "链接失败");
        }

        @Override
        public void illegal() {
            Log.e("wl", "非法调用");
        }
    };

    private ReadCardCallback readCardCallback = new ReadCardCallback.Stub() {

        @Override
        public void onCardDetected(CardInfo cardInfo) throws RemoteException {
            //LogUtil.e(TAG, "onCardDetected:" + cardInfo.toString());
            Message message = new Message();
            message.what = 0x01;
            message.obj = cardInfo;
            mHandler.sendMessage(message);
        }

        @Override
        public void onError(int i, String s) throws RemoteException {
            //LogUtil.e(TAG, "onError:" + i + " " + s);
            Message message = new Message();
            message.what = 0x02;
            message.obj = "code:" + i + " message:" + s;
            mHandler.sendMessage(message);
        }

        @Override
        public void onStartCheckCard() throws RemoteException {
            //LogUtil.e(TAG, "onStartCheckCard:正在检卡");
        }
    };

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    CardInfo cardInfo = (CardInfo) msg.obj;
                    if (callback != null) {
                        callback.refreshCardNo(Coverter.cardNo8To10(cardInfo.uuid));
                    }
                    break;
                case 0x02:
//                    String str = getString(R.string.check_card_error) + msg.obj;
//                    tvCardInfo.setText(str);
                    break;
            }
        }

    };

}
